﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Youz.Common.Helper
{
    public class WebRequestHelper
    {

        public static string GetResponseTextPOST(string url, string data)
        {
            return GetResponseText(url, "POST", "application/x-www-form-urlencoded", ".NET Framework Client", null, data, Encoding.UTF8);
        }

        public static string GetResponseTextGET(string url)
        {
            return GetResponseText(url, "GET", "text/html", ".NET Framework Client", null, null, Encoding.UTF8);
        }
        public static string GetResponseTextGET(string url, Encoding requestEnc)
        {
            return GetResponseText(url, "GET", "text/html", ".NET Framework Client", null, null, requestEnc);
        }

        public static HttpWebResponse GetResponseStream(string url, string method, string requestContentType, string userAgent, Dictionary<string, string> head, string data, Encoding requestEnc)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = method;
            request.ContentType = requestContentType;
            request.Timeout = 30000;//30秒
            request.UserAgent = userAgent;
            if (head != null && head.Count > 0)
            {
                foreach (var item in head)
                {
                    request.Headers.Add(item.Key, item.Value);
                }
            }
            if (string.IsNullOrEmpty(data) == false)
            {
                byte[] dataByte = requestEnc.GetBytes(data);
                request.ContentLength = dataByte.Length;
                Stream writer = request.GetRequestStream();
                writer.Write(dataByte, 0, dataByte.Length);
                writer.Close();
            }
            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                return response;
            }
            catch (WebException ex)
            {
                //return null;
                throw new Exception(string.Format("message:{0};status:{1};url:{2};data:{3}", ex.Message, ex.Status, url, data));
            }
        }

        public static string GetResponseText(string url, string method, string requestContentType, string userAgent, Dictionary<string, string> head, string data, Encoding requestEnc)
        {
            HttpWebResponse response = GetResponseStream(url, method, requestContentType, userAgent, head, data, requestEnc);
            if (response != null)
            {
                Stream responseStream = responseStream = response.GetResponseStream();
                if (response.ContentEncoding.ToLower().Contains("gzip"))
                    responseStream = new GZipStream(responseStream, CompressionMode.Decompress);
                else if (response.ContentEncoding.ToLower().Contains("deflate"))
                    responseStream = new DeflateStream(responseStream, CompressionMode.Decompress);

                StreamReader sr = new StreamReader(responseStream, requestEnc);

                return sr.ReadToEnd();
            }
            return "";
        }
    }
}
